<template>
  <div class="search-container">
    <!-- 搜索栏 -->
    <form action="/">
      <van-search
        v-model="searchText"
        show-action
        shape="round"
        background="red"
        placeholder="请输入搜索关键词"
        @search="onSearch"
        @cancel="onCancel"
        @focus="isResultShow = false"
      />
      <div class="recommend">
        <a href="http://localhost:8080/#/applyOpenStore">申请开店</a>
        <a href="http://localhost:8080/#/promotion">推广赚钱</a>
      </div>
    </form>
    <!-- /搜索栏 -->

    <!-- 搜索历史记录 -->
    <search-history
      v-if="isResultShow"
      :lists="lists"
      @clearAll1="lists=[]"
      @clearOne1="lists.splice($event,1)"
    />
    <!-- /搜索历史记录 -->

    <!-- 历史记录 -->
    <search-result v-else @search="onSearch" />
    <!-- /历史记录 -->
  </div>
</template>

<script>
import SearchHistory from './components/search-history'
import SearchResult from './components/search-result'
import { setItem, getItem } from '@/utils/storage'
export default {
  name: 'SearchIndex',
  components: {
    SearchHistory,
    SearchResult
  },
  props: {},
  data () {
    return {
      searchText: '',
      isResultShow: false,
      lists: getItem('lists') || []
      // searchHistories: getItem('TOUTIAO_SEARCH_HISTORIES') || [] // 搜索的历史记录数据
    }
  },
  watch: {},
  methods: {
    onSearch (val) {
      this.lists.unshift(val)
      setItem('lists', this.lists)
      this.searchText = ''
      this.isResultShow = true
    },
    onCancel () {
      this.$router.back()
    }
  }
}
</script>

<style scoped lang="less">
.search-container {
  position: relative;
  .recommend {
    position: absolute;
    top: 33px;
    right: 120px;
    font-size: 30px;
    a {
      color: rgb(111, 140, 254);
    }
  }
  .recommend a:nth-child(2) {
    margin-left: 10px;
  }
  .van-search__action {
    color: #fff;
  }
}
</style>
